package it.polimi.swKnights.SWIMv2.servlets.users;

import it.polimi.swKnights.SWIMv2.ejb.exceptions.InvalidLoginPrivilegesException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.LoginFailException;
import it.polimi.swKnights.SWIMv2.ejb.friendsEngine.FriendshipRequestManagerRemote;
import it.polimi.swKnights.SWIMv2.general.Errors;
import it.polimi.swKnights.SWIMv2.servlets.SwimServlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.ejb.EJBException;
import javax.persistence.EntityNotFoundException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class AcceptFriendProcessData
 */
public class AcceptFriendProcessData extends SwimServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		String userName = (String) session.getAttribute("userName");
		String password = (String) session.getAttribute("password");
		HashMap<String, Boolean> acceptedFriends = getSelectedRequests(request);

		try
		{
			this.handleRequest(userName, password, acceptedFriends);
			response.sendRedirect("./addFriends?added="
					+ this.getAcceptedFriendsList(acceptedFriends));
		}
		catch (EJBException e)
		{
			response.sendRedirect("./addFriends?error=" + Errors.notfound);
		}
		catch (LoginFailException e)
		{
			response.sendRedirect("./siteHome?error=" + Errors.session);
		}
		catch (InvalidLoginPrivilegesException e)
		{
			response.sendRedirect("./siteHome?error=" + Errors.session);
		}
	}

	private String getAcceptedFriendsList(
			HashMap<String, Boolean> acceptedFriends)
	{
		String separator = "";
		StringBuilder builder = new StringBuilder();
		for (Entry<String, Boolean> requestResponse : acceptedFriends
				.entrySet())
		{
			if (requestResponse.getValue())
			{
				builder.append(separator);
				builder.append(requestResponse.getKey());
				separator = ", ";
			}
		}
		
		return builder.toString();
	}

	private HashMap<String, Boolean> getSelectedRequests(
			HttpServletRequest request)
	{
		HashMap<String, Boolean> acceptedFriends = new HashMap<String, Boolean>();

		Map<?, ?> params = request.getParameterMap();
		Iterator<?> i = params.keySet().iterator();

		while (i.hasNext())
		{
			String attribName = (String) i.next();
			String attribValue = ((String[]) params.get(attribName))[0];

			if (attribName.startsWith("radio_"))
			{
				
				acceptedFriends.put(attribName.substring(6),
						this.getTruthFromRadio(attribValue));
			}
		}
		return acceptedFriends;
	}

	private void handleRequest(String userName, String password,
			HashMap<String, Boolean> acceptedFriends)
			throws EntityNotFoundException, LoginFailException,
			InvalidLoginPrivilegesException
	{
		FriendshipRequestManagerRemote requestManager = this
				.lookUp(FriendshipRequestManagerRemote.class);

		for (Entry<String, Boolean> requestResponse : acceptedFriends
				.entrySet())
		{
			if (requestResponse.getValue())
			{
				requestManager.acceptRequest(requestResponse.getKey(),
						userName, password);
			}
			else
			{
				requestManager.refuseRequest(requestResponse.getKey(),
						userName, password);
			}
		}
	}

}
